from machine import SoftI2C
from lib.config.index import sensor_config
from utils import format, device

sensors0 = sensor_config.get_sensor("ssd1306")
print(sensors0)
scl = sensors0["scl"]
sda = sensors0["sda"]
device.running_ticks_us("ssd1306_http.0")
# You can choose any other combination of I2C pins
# i2c = SoftI2C(scl, sda)
# device.running_ticks_us("ssd1306_http.1")
# print("I2C SCANNER")
# devices = i2c.scan()

# if len(devices) == 0:
#     print("No i2c device !")
# else:
#     print("i2c devices found:", len(devices))

#     for d in devices:
#         print("I2C hexadecimal address: ", hex(d))


def demo():
    from lib.sensors.ssd1306.oled import OLED
    device.running_ticks_us("ssd1306_http.2")
    from lib.api import ssd1306
    device.running_ticks_us("ssd1306_http.2.1")
    text = "韩小信54321"
    data_bytes = ssd1306.get_ssd1306_fonts12(text.encode("utf-8"))
    device.running_ticks_us("ssd1306_http.3")
    fonts = format.format_network_response(data_bytes)
    device.running_ticks_us("ssd1306_http.3.1")
    print(fonts)
    font12 = fonts
    _oled = OLED(scl, sda)
    _oled.font_renderer.set_font(font12, 12)
    _oled.font_renderer.reset_line()
    _oled.font_renderer.show_text(text)
    device.running_ticks_us("ssd1306_http.4")


if __name__ == "__main__":
    demo()
